"use strict";
/* Controllers */
app.controller("fieldMonitorController", ['$scope', '$location', '$rootScope', '$routeParams', 'fieldMonitorService', function ($scope, $location, $rootScope, $routeParams, fieldMonitorService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};

    vm.p.metric = $routeParams.metric;
    vm.p.biz = $routeParams.biz;
    vm.p.actionType = $routeParams.actionType;
    vm.p.logsource = $routeParams.logsource;

    var errorFn = function (result) {
        alert(result);
    };

    $scope.loadChart = function() {
        $scope.loadMetaData();

        if(vm.p.logsource=='0') {
            return;
        }
        $("#refreshTable").addClass('disabled');
        $scope.loadMetricTrend();
        $scope.loadIncMetricTrend();
        $scope.loadRatioMetricTrend();
        $("#refreshTable").removeClass('disabled');
    }

    $scope.loadMetaData = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        fieldMonitorService.loadMetaData(params, procFn, errorFn);
    };

    $scope.loadMetricTrend = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $scope.initChart('total_trend', data);
        };
        fieldMonitorService.loadMetricTrend(params, procFn, errorFn);
    };
    $scope.loadIncMetricTrend = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $scope.initChart('inc_trend', data);
        };
        fieldMonitorService.loadIncMetricTrend(params, procFn, errorFn);
    };

    $scope.loadRatioMetricTrend = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) return;
            $scope.initChart('ratio_trend', data);
        };
        fieldMonitorService.loadRatioMetricTrend(params, procFn, errorFn);
    };

    $scope.initChart = function(id, rs) {
        var dom = document.getElementById(id);
        var myChart = echarts.init(dom);
        window.onresize = function () {
            myChart.resize();
        };
        var series = [];
        var arr = rs.values;
        for(var i=0;i<arr.length;++i) {
            var item = {
                name: arr[i].name,
                yAxisIndex: 0,
                data: arr[i].data,
                type: 'line',
                smooth: false
            };
            series.push(item);
        }

        var app = {};
        var option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { type: 'cross' }
            },
            legend: {
            },
            dataZoom: [{}],
            // 时间序列
            xAxis: {
                type: 'category',
                data: rs.xAxisData
            },
            yAxis: [{
                    name: '指标',
                    type: 'value'
                }
            ],
            series: series
        };
        window.onresize = function () {
            myChart.resize();
        };
        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

    $scope.initializeController = function () {
        $scope.vm.result.ms = '';

        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });

        if (vm.p.metric!='0') {
            $scope.loadChart();
        }
    };
    $scope.initializeController();
}]);


/* Services */
app.service('fieldMonitorService', ['ajaxService', function (ajaxService) {


    this.loadMetaData = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/field_monitor/loadMetaData.do", successFunction, errorFunction);
    };

    this.loadMetricTrend = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/field_monitor/loadMetricTrend.do", successFunction, errorFunction);
    };
    this.loadIncMetricTrend = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/field_monitor/loadIncMetricTrend.do", successFunction, errorFunction);
    };
    this.loadRatioMetricTrend = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/field_monitor/loadRatioMetricTrend.do", successFunction, errorFunction);
    };
}]);

function queryOptFmt(value, row, index) {
    return [
        '<a href="#/sre/metric2/field_monitor/', row.metricKey, '/', row.biz, '/', row.actionType, '/', row.logsource, '" target="_blank">', " <u>查询趋势</u>", '</a>'
    ].join('');
}